<?php

defined('BASEPATH') or die('forbidden');

class Attribute_Controller Extends Controller {

    private $rolename;
    private $inc;
    private $m;	//数据库连接
	private $professionList;	//职业列表
    private $server_info;
	
    function _init() {
        check_level(50, segment(0), segment(1));
        set_time_limit(0);
        $this->inc = $this->config['inc'];
        $this->professionList = $this->inc['type_proffessionid'];
        //得到选择的区号
	$areanum = $_SESSION['area_selected_id'];
        if ($areanum == '') 
        {
            echo "<script>alert('" . translate('C_SELECT_AREA_TEXT') . "');</script>";
            return;
        }

        //query server mapping
        $selectSql = "SELECT * FROM server_mapping WHERE dbtype='1' AND areanum={$areanum} ";
        //$selectSql = "SELECT * FROM server_mapping WHERE dbtype='1'";
        $this->db->query($selectSql);
        $this->server_info=$this->db->get_array('default');

        // db数据库
        $this->m = new MongoClient("mongodb://{$this->server_info['username']}:{$this->server_info['userpwd']}@{$this->server_info['dbip']}");

//        $collection=$this->m->{$this->server_info['dbname']}->PlayerSimple;
//        $document=$collection->find();
//        var_dump($document);
//        $data=array();
//       // $total = count($document);
//
//        foreach ($document as $key=>$value){
//            $value['logofftime'] = get_datetime($value['logofftime']);
//            $data[$key]=array('id' => $value['_id'], 'cell' => $value);
//            echo $value['_id'];
//        }
//        var_dump($data);
//        exit();
    }

    function index() {
        $this->view->display("dbplayer/attribute/index");
    }

    function flexigrid() {

        list($colkey, $colsinfo, $where, $sortname, $sortorder, $offset, $rp, $page) = $this->common->get_flexigrid_params();

        $query=$_POST['query'];
        $query_type=$_POST['qtype'];
        if($query_type=='_id'){
            $filter=array($query_type=>(float)$query);
        }else{
            $filter=array($query_type=>$query);
        }
        $collection=$this->m->{$this->server_info['dbname']}->PlayerSimple;
        if($query!=''){
            $document=$collection->find($filter);
        }else{
            $document=$collection->find();
        }
        $data=array();
        $total = count($document);

        foreach ($document as $key=>$value){
            $value['logofftime'] = get_datetime($value['logofftime']);
            //$value['loginday'] = get_datetime($value['loginday']);
            $data[$key]=array('id' => $value['_id'], 'cell' => $value);
        }

//        //$this->db->connect('111.75.194.177','root','654321');
//        //200090016 表示 充值
//        $sql = "SELECT COUNT(*) AS num FROM player as A where {$where} ";
//        //echo " sql2:$sql ";
//        $one = $this->dbConnt->one($sql);
//        $total = $one['num'];
//        $orderby = $sortname ? "ORDER BY {$sortname} {$sortorder} " : "";
//
//        $sql = "SELECT {$colsinfo} FROM player where {$where}   {$orderby} LIMIT {$offset},{$rp}";
//        $this->dbConnt->query($sql);
//        //$id = 0;
//        while ($n = $this->dbConnt->get_array('default')) {
//
//        	$n['profession']=$this->professionList[$n['profession']]."(".$n['profession'].")";
//        	//$n['profession_type']=$this->professionList[$n['profession_type']]."(".$n['profession_type'].")";
//            //$n['indate']=get_datetime($n['indate']);
//            $rows[] = array('id' => $n['id'], 'cell' => $n);
//            //$id = $id+1;
//        }
        //var_dump($rows);
        $result = array(
            'page' => $page,
            'total' => $total,
            'rows' => $data
        );

        //$this->dbConnt->free_all();
        echo json_encode($result);
    }

}

?>
